redis客户端的使用

下载

找相应语言的下载(一般选择有笑脸和星星的)

1
https://redis.io/clients#php

image-20190802165054046

image-20190802165151233

image-20190802165205236

image-20190802165214466

image-20190802165223779


image-20190802165239840


image-20190802165257444

image-20190802165322966


image-20190802165339269


image-20190802165353704


image-20190802165404647


Jedis直连

执行流程

  1. 创建Jedis对象
  2. 通过Jedis执行命令
  3. 返回Jedis执行结果
  4. 关闭Jedis连接

创建Jedis的方式

1
2
3
4
5
6
7
/**
* @param host Redis节点所在机器的IP或域名
* @param port Redis服务的端口号
* @param connectionTimeout 客户端连接超时时间(毫秒)
* @param soTimeout 客户端读写超时时间(毫秒)
*/
public Jedis(String host , int port , int connectionTimeout , int soTimeout)

Jedis连接池

执行流程

  1. 创建一个JedisPool对象
  2. 从资源池中获取一个Jedis对象
  3. 通过Jedis执行命令
  4. 返回Jedis执行结果
  5. 关闭Jedis连接,将Jedis还给资源池

创建Jedis连接池的方式

1
2
3
4
JedisPoolConfig config = new JedisPoolConfig();
JedisPool jedisPool = new JedisPool(config , "127.0.0.1" , 6379);
Jedis jedis = jedisPool.getResource();
jedis.close();

Jedis直连 VS Jedis连接池

优点 缺点
Jedis直连 使用简单
适用于少量长期连接的场景
存在每次新建/关闭TCP连接的开销
资源无法控制,存在连接泄露的风险
Jedis对象线程不安全
Jedis连接池 Jedis对象预先生成,降低使用开销
连接池的形式保护和控制资源的使用
相对于直连,使用相对麻烦
尤其在资源的管理上需要许多参数保证
一旦参数不合理会出现很多问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @(#)JedisTest.java, 2018/12/26.
* <p/>
* Copyright 2018 Netease, Inc. All rights reserved.
* NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.chapter03Jedis;

import com.google.common.base.Joiner;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Test;
import redis.clients.jedis.*;

import java.util.*;

public class JedisTest {
@Test
public void testJedisCluster() {
Set<HostAndPort> nodeList = new HashSet<>();
nodeList.add(new HostAndPort("127.0.0.1" , 7000) );
nodeList.add(new HostAndPort("127.0.0.1" , 7001) );
nodeList.add(new HostAndPort("127.0.0.1" , 7002) );
nodeList.add(new HostAndPort("127.0.0.1" , 7003) );
nodeList.add(new HostAndPort("127.0.0.1" , 7004) );
nodeList.add(new HostAndPort("127.0.0.1" , 7005) );
JedisPoolConfig poolConfig = new JedisPoolConfig();
int timeout = 30_000;
JedisCluster jedisCluster = new JedisCluster(nodeList , timeout , poolConfig);
jedisCluster.set("hello" , "world");
System.out.println(jedisCluster.get("hello"));

}

@Test
public void testConnectRedis() {
Jedis jedis = new Jedis("127.0.0.1" , 6379);
System.out.println(jedis.ping());
jedis.set("hello" , "world");
System.out.println(jedis.get("hello"));
}

@Test
public void testJedisPool() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisPool jedisPool = new JedisPool(poolConfig , "127.0.0.1" , 6379);
Jedis jedis = jedisPool.getResource();
System.out.println(jedis.ping());
jedis.set("hello" , "world");
System.out.println(jedis.get("hello"));
}

@Test
public void testWithoutPipeline() {
Jedis jedis = new Jedis("127.0.0.1" , 6379);
for(int i = 0; i < 10000 ; i++ ) {
jedis.hset("hashKey-" + i , "field-" + i , "value-" + i);
}
}

@Test
public void testPipeline() {
Jedis jedis = new Jedis("127.0.0.1" , 6379);
for(int i = 0 ; i < 100 ; i++ ) {
Pipeline pipeline = jedis.pipelined();
for(int j = i * 100 ; i < (i+1) * 100 ; j++ ) {
pipeline.hset("hashKey-" + i , "field-" + i , "value-" + i);
}
pipeline.syncAndReturnAll();
}
}

@Test
public void testSubscribe() {
Jedis jedis = new Jedis("127.0.0.1" , 6379);
jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
System.out.println("receive channel = [" + channel + "] message = [" + message + "]");
}
} , "aliTV" , "googleTV");
}

@Test
public void testPublish() {
Jedis jedis = new Jedis("127.0.0.1" , 6379);
jedis.publish("aliTV" , "I am xuyinan");
jedis.publish("googleTV" , "My age is 27");
}

@Test
public void testSentinelPool() {
Set<String> sentinelSet = new HashSet<String>() {{
add("127.0.0.1:26379");
add("127.0.0.1:26380");
add("127.0.0.1:26381");
}};
JedisPoolConfig poolConfig = new JedisPoolConfig();
String masterName = "myMaster";
int timeout = 30_000;
JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName , sentinelSet , poolConfig , timeout);
Jedis jedis = sentinelPool.getResource();
jedis.set("hello" , "world");
System.out.println(jedis.get("hello"));
jedis.close();
}


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
package com.chapter03Jedis;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnel;
import com.google.common.hash.Funnels;
import org.hamcrest.core.Is;
import org.junit.Test;

import java.nio.charset.Charset;


public class BloomFilterTest {

@Test
public void testGuava() {
Funnel<Integer> funnel = Funnels.integerFunnel();
int size = 1000000;
double errorChance = 0.001; //错误率
BloomFilter<Integer> filter = BloomFilter.create(funnel , size , errorChance);
for(int i = 0 ; i < size ; i++) {
filter.put(i);
}

for(int i = 0 ; i < size ; i++ ) {
if( !filter.mightContain(i) ) {
System.out.println("发现不存在的数据 : " + i);
}
}
}
}

image-20190802165642162

image-20190802165654872

image-20190802165715033

image-20190802165727384

image-20190802165741960